*** One-Child Policy, Marriage Distortion, and Welfare Loss ***
*** by Wei Huang, Yinghao Pan and Yi Zhou ***

clear all 
set more off
set scheme s1color
cap log close 
graph set window fontface "Times New Roman"

set niceness 10
gl MAIN = "F:\Dropbox"

gl path1 = "$MAIN/Census"
gl path2 = "$MAIN/Marriage/Working_data"
gl path3 = "$MAIN/Marriage/Restat/dataverse_files/Results" 
gl path4 = "$MAIN/Marriage/Restat/dataverse_files/Graphics" 
gl CONTROL = " year_birth sex#age urban_hk prov_hk##c.(year_birth ) "



use "$path2/unmarried", clear
keep if relation == 0 | relation == 1 | relation == 2 | relation == 6 


merge 1:1 wave area hhid name relation using "$path2/min_marr_policy", keepusing(marry_1 marry_2 marry_3)


drop if mi(hktype)
replace urban = hktype == 1 
su age 

gen han = 1- minority 

replace unmarried = unmarried * 100 
gen unmarr_25 = marr_age > 25 if !mi(marr_age)
replace unmarr_25 = 1 if unmarried == 100 
replace unmarr_25 = unmarr_25 *100 

gen urban_hk = hktype == 2 
gen senior = educ >= 4

egen clsid = group(prov_hk year_birth)


gen marry_9 = unmarried == 0 & (mi(marry_1) & mi(marry_2) & mi( marry_3))

recode marry_1 marry_2 marry_3 (. = 0) 

gen HH = marry_1 * 100 
gen HM = marry_2 * 100 
gen MM = marry_3 * 100 
gen Other = marry_9 * 100 
gen HOMO = HH + MM
su HOMO

keep if age <= 55 
keep if age > 25
keep if year_birth <= 1980 
keep if Other == 0 
tab maritus

keep if sex == 2 // women 

egen n_birth = rowtotal(numbirm numbirf)
replace n_birth = . if age > 45




cap erase "$path3/birth_fine_prov.xls"
cap erase "$path3/birth_fine_prov.txt"


*** Table 2 ***

foreach policy in "1" "mino_policy == 1"  "mino_policy == 0"  {
foreach eth in  "han" "han == 0"{
preserve 
keep if n_birth != . & `policy' & `eth'



reghdfe n_birth fine_16_25  [aw = aw], a($CONTROL)  cluster(prov_hk)
su n_birth [aw = aw]
outreg2 using "$path3/birth_fine_prov.xls", append bracket addstat(Y, `r(mean)') ///
 ctit(`policy' & `eth') keep(fine_16_25)  dec(3)


restore


}
}




cap drop maritus 
gen maritus = 0 if unmarried == 100
replace maritus = 1 if HOMO == 100|HM == 100 




*** Appendix Table A5 ***



preserve 
keep if HH == 100  


reghdfe n_birth fine_16_25  [aw = aw], a($CONTROL)  cluster(prov_hk)
su n_birth [aw = aw]
outreg2 using "$path3/birth_fine_prov.xls", append bracket addstat(Y, `r(mean)') ///
ctit(HH == 100 )   dec(3)


restore


foreach policy in "1"  "mino_policy == 0" "mino_policy == 1"   {
preserve 
keep if HM == 100 & `policy' 


reghdfe n_birth fine_16_25  [aw = aw], a($CONTROL)  cluster(prov_hk)
su n_birth [aw = aw]
outreg2 using "$path3/birth_fine_prov.xls", append bracket addstat(Y, `r(mean)') ///
ctit(HM == 100 & `policy'  )   dec(3)


restore


}



preserve 
keep if MM == 100


reghdfe n_birth fine_16_25  [aw = aw], a($CONTROL)  cluster(prov_hk)
su n_birth [aw = aw]
outreg2 using "$path3/birth_fine_prov.xls", append bracket addstat(Y, `r(mean)') ///
ctit(MM == 100 )   dec(3)


restore


*** Appendix Figure A10(e)-(f) ***
*** Fertility, Figure for residuals **
preserve 
foreach y_var in "n_birth" {
cap drop `y_var'_han_res fine_han_res `y_var'_mino_res fine_mino_res
reghdfe `y_var' if han [aw = aw], a( year_birth sex#age urban_hk prov_hk##c.(year_birth )) res(`y_var'_han_res)
reghdfe fine_16_25 if han [aw = aw], a( year_birth sex#age urban_hk prov_hk##c.(year_birth )) res(fine_han_res)
reghdfe `y_var' if han == 0 [aw = aw], a( year_birth sex#age urban_hk prov_hk##c.(year_birth ) ) res(`y_var'_mino_res)
reghdfe fine_16_25 if han == 0 [aw = aw], a( year_birth sex#age urban_hk prov_hk##c.(year_birth ) ) res(fine_mino_res)

reg `y_var'_han_res fine_han_res if han [aw = aw], cluster(prov_hk)
local beta = string(_b[fine_han_res],"%9.3f")
local cons = string(_b[_cons],"%9.3f")
local se = string(_se[fine_han_res], "%9.3f")
binscatter n_birth_han_res fine_han_res if han [aw = aw], n(30) xlabel(-0.8(0.4)0.8) ylabel(-0.06(0.02)0.06) ///
ytit("Number of births, Residuals") xtit("Fines, Residuals") text(-0.03 0 "Y =  `beta'X + `cons'" "(`se')" )
gr export "$path4/n_birth_fine_han_res.pdf", replace  

reg `y_var'_mino_res fine_mino_res if han == 0 [aw = aw], cluster(prov_hk)
local beta = string(_b[fine_mino_res],"%9.3f")
local cons = string(_b[_cons],"%9.3f")
local se = string(_se[fine_mino_res], "%9.3f")
 binscatter n_birth_mino_res fine_mino_res if han ==0 [aw = aw], n(30)  xlabel(-0.8(0.4)0.8) ylabel(-0.06(0.02)0.06) ///
 ytit("Number of births, Residuals") xtit("Fines, Residuals") text(-0.03 0 "Y =  `beta'X + `cons'" "(`se')" )
 gr export "$path4/n_birth_fine_mino_res.pdf", replace  

}
restore


*}
